package main

import (
	"html/template"
	"net/http"
)

func main() {
	http.HandleFunc("/index", index)
	http.HandleFunc("/login", login)
	http.ListenAndServe(":8080", nil)
}

func index(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		t, _ := template.ParseFiles("view/index.html")
		t.Execute(w, nil)
	}
}

func login(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		account := r.FormValue("username")
		password := r.FormValue("password")
		if account == "admin" && password == "123456" {
			w.Write([]byte("<script>confirm('登录成功！');</script>"))
			t, _ := template.ParseFiles("view/home.html")
			t.Execute(w, nil)
		} else {
			w.Write([]byte("<script>alert('用户名或密码错误！');</script>"))
			t, _ := template.ParseFiles("view/index.html")
			t.Execute(w, nil)
		}
	}
}
